package cloud.xlh.filedynamicawareness;

import cloud.xlh.filedynamicawareness.utils.FileUtil;
import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.*;
import java.util.function.Consumer;
import java.security.MessageDigest;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : example
 * @ Author: XuLeHuang
 * @ Date: 2023/1/22 15:38
 * @ Description:
 */
public class ClientWorker {
    final ScheduledExecutorService executor;
    final ScheduledExecutorService executorService;
    private final HashMap<String, List<String[]>> cacheMap = new HashMap();

    public ClientWorker(Properties properties) {
        this.executorService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("cloud.xlh.client.Worker.longPolling.fileDynamicAwareness");
                t.setDaemon(true);
                return t;
            }
        });
        this.executor = Executors.newScheduledThreadPool(1, new ThreadFactory() {
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("cloud.xlh.client.Worker.fileDynamicAwareness");
                t.setDaemon(true);
                return t;
            }
        });
        this.executor.scheduleWithFixedDelay(new Runnable() {
            public void run() {

                    ClientWorker.this.checkConfigInfo();


            }
        }, 1L, 10L, TimeUnit.MILLISECONDS);

        new Thread(() -> {
            while (true) {
                List<String[]> csvFile = cacheMap.get("csvFile");
                //System.out.println("开始");
                if (csvFile != null) {
                    csvFile.stream().forEach(new Consumer<String[]>() {
                        @Override
                        public void accept(String[] strings) {
                            try {
                                Thread.sleep(10000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(strings[0]);
                        }

                    });
                }
                //System.out.println("结束");
            }
        }).start();
    }

    public void checkConfigInfo() {
        this.executorService.execute(new ClientWorker.LongPollingRunnable(0));

    }

    class LongPollingRunnable implements Runnable {
        private final int taskId;

        public LongPollingRunnable(int taskId) {
            this.taskId = taskId;
        }

        public void run() {
            //获取csv文件
            String filePath = "D:\\development\\environment\\idea\\IdeaProjects\\example\\file-dynamic-awareness\\src\\main\\resources\\list.csv";

            List<String[]> list = FileUtil.readFromCsvFile(filePath);
            //cacheMap.put("csvFile" , list);
            StringBuilder stringBuilderFromIO = new StringBuilder("");
            list.stream().forEach(new Consumer<String[]>() {
                @Override
                public void accept(String[] strings) {
                    stringBuilderFromIO.append(strings[0]);
                }
            });
            //System.out.println(stringBuilderFromIO);
            StringBuilder stringBuilderFromCache = new StringBuilder("");
            List<String[]> csvFile = cacheMap.get("csvFile");
            //System.out.println(csvFile);
            if (csvFile == null) {
                cacheMap.put("csvFile" , list);
            }
            try {
                String strFromIO = DigestUtils.md5DigestAsHex(stringBuilderFromIO.toString().getBytes("UTF-8"));
                System.out.println(stringBuilderFromIO.toString());
                String strFromCache = DigestUtils.md5DigestAsHex(stringBuilderFromCache.toString().getBytes("UTF-8"));
                System.out.println(stringBuilderFromIO.toString());
                System.out.println(strFromIO.equals(strFromCache));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

           /* list.stream().forEach(new Consumer<String[]>() {
                @Override
                public void accept(String[] strings) {
                    System.out.println(strings);
                }
            });
            System.out.println(cacheMap.size());*/

            //对比内存中的
            //更新
        }
    }
}
